package com.hs.bai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.bai.dto.DispatchCenterDto;
import com.hs.ding.pojo.Dispatch;
import com.hs.ding.service.IDispatchService;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.hu.pojo.Receipt;
import com.hs.hu.service.IReceiptService;
import com.hs.lin.pojo.ExceptionRegistration;
import com.hs.lin.service.IExceptionRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Bai Dingkun
 * @date 2022-06-17
 * @desc 物流信息查询
 */
@CrossOrigin
@RestController
@RequestMapping("/logistics")
public class LogisticsController {

    @Autowired
    private IDispatchService dispatchService;
    @Autowired
    private IReceiptService receiptService;
    @Autowired
    private IExceptionRegistrationService exceptionRegistrationService;

    /**
     * 多表分页查询
     * @return
     */
    @GetMapping("/page")
    public ResultBean findInfoByPage(@RequestParam Integer currentPage, @RequestParam Integer pageSize){

        List<Dispatch> list = dispatchService.findDealDispatch();

        PageBean<DispatchCenterDto> pageBean = new PageBean<>();
        List<DispatchCenterDto> dtoList = new ArrayList<>();

        for (int i = 0; i < pageSize; i++) {
//            分页限制
            int index = (currentPage - 1) * pageSize + i;
            if (index >= list.size()){
                break;
            }
            DispatchCenterDto dto = new DispatchCenterDto();
            Dispatch dispatch = list.get(index);
            dto.setDispatchId(dispatch.getdOrderId());
            dto.setTransType(dispatch.getTransport().getNeed().getTransType());
            dto.setDispatchRoute(dispatch.getTransport().getNeed().getRoute());
            dto.setDispatchCreateTime(dispatch.getTransport().getNeed().getCTime());
            dto.setDispatchAppointTime(dispatch.getTransport().getNeed().getCTime());
            dto.setDispatchCarType(dispatch.getTransport().getNeed().getGType());
            dto.setDispatchCarPlate(dispatch.getTransport().getCarPlate());
            dto.setDispatchCarCarrier(dispatch.getTransport().getCarrierName());
            QueryWrapper<Receipt> qw = new QueryWrapper<>();
            qw.eq("r_order_id",dispatch.getTransport().getNeed().getOrderId());
            Receipt receipt = receiptService.getOne(qw);
            dto.setRTime(receipt.getrTime());
//            QueryWrapper<ExceptionRegistration> qw2 = new QueryWrapper<>();
//            qw2.eq("c_id",dispatch.getdOrderId());
//            ExceptionRegistration registration = exceptionRegistrationService.getOne(qw2);
//            dto.setDispatchNode(registration.getNode());
            dtoList.add(dto);
        }

        pageBean.setList(dtoList);
        pageBean.setTotal(list.size());
        ResultBean ok = ResultBean.OK();
        ok.setData(pageBean);
        return ok;

    }

}
